// Grc1leg is needed for graphs. To install:
// net install grc1leg,from( http://www.stata.com/users/vwiggins/) 

// change the working directory// change the working directory to the project director:
// import data
use "data_main_aan25_militias_neg.dta", clear

// Variable Lists:
// Main explanatory variables: alternative measurements
global xx1 Mil_vs_Reb_dm_lag Mil_vs_Civ_lag Mil_vs_Gov_lag 
global xx2 Mil_vs_Reb_oth_lag
global xx3 Mil_vs_Mil_lag

// Core control variables
global core impgdppcln imppopln c.v2x_polyarchy dydurln 
global vio_ag_civ violence_civ_reb_lag_ln violence_civ_gov_lag_ln
global intensity  Intensity_rate_lag brd_d_ln 
global cont2 rebno_ct lull UN Ethnicincomp 
global cont3 PrevMed_5y RebTerCont RebPolWingLeg RebExpSup Internationalized
global ts tsln tsln2 tsln3


// Main Models : TABLE 2
// Model 1
logit Negotiation $xx1 $core $intensity $ts, robust cluster(DyadId)
estat ic
mat es_ic = r(S)
local LL: display %4.1f es_ic[1,3]
local AIC: display %4.1f es_ic[1,5]
local BIC: display %4.1f es_ic[1,6]
est store m1
outreg2 m1 using table_all.doc, alpha(0.001, 0.01, 0.05, 0.10) symbol(***, **, *, +) stats(coef se) dec(2) addstat(LL, `LL', AIC, `AIC', BIC, `BIC') replace label sideway 


// Model 2
logit Negotiation $xx1 $xx2 $core $intensity $cont2 $ts, robust cluster(DyadId)
estat ic
mat es_ic = r(S)
local LL: display %4.1f es_ic[1,3]
local AIC: display %4.1f es_ic[1,5]
local BIC: display %4.1f es_ic[1,6]
est store m2
outreg2 m2 using table_all.doc, alpha(0.001, 0.01, 0.05, 0.10) symbol(***, **, *, +) stats(coef se) dec(2) addstat(LL, `LL', AIC, `AIC', BIC, `BIC') append label sideway 

// Model 3
logit Negotiation $xx1 $xx2 $xx3 $core $intensity $cont2 $vio_ag_civ  $ts, robust cluster(DyadId)
estat ic
mat es_ic = r(S)
local LL: display %4.1f es_ic[1,3]
local AIC: display %4.1f es_ic[1,5]
local BIC: display %4.1f es_ic[1,6]
est store m3
outreg2 m3 using table_all.doc, alpha(0.001, 0.01, 0.05, 0.10) symbol(***, **, *, +) stats(coef se) dec(2) addstat(LL, `LL', AIC, `AIC', BIC, `BIC') append label sideway 

// Model 4
logit Negotiation $xx1 $xx2 $xx3 $core $intensity $cont2 $vio_ag_civ $cont3 $ts, robust cluster(DyadId)
estat ic
mat es_ic = r(S)
local LL: display %4.1f es_ic[1,3]
local AIC: display %4.1f es_ic[1,5]
local BIC: display %4.1f es_ic[1,6]
est store m4
outreg2 m4 using table_all.doc, alpha(0.001, 0.01, 0.05, 0.10) symbol(***, **, *, +) stats(coef se) dec(2) addstat(LL, `LL', AIC, `AIC', BIC, `BIC') append label sideway 


// Figure 2: Predicted Probability of Peace Talks by Different Types of Militia Violence
// Figure 2 Left Pane
logit Negotiation $xx1 $xx2 $xx3 $core $intensity $cont2 $cont3  $ts, robust cluster(DyadId)
margins, at(Mil_vs_Reb_dm_lag =(0 (5) 80) Mil_vs_Civ_lag = 0  Mil_vs_Gov_lag = 0  ) 
marginsplot , recast(line) ciopt(color(%20)) recastci(rarea) graphregion(color(white)) title("") ytitle("") xtitle("Against Rebels (t-1)", size(small)) yscale(range(0 (0.1) 0.8 )) ylabel(0 [0.1] 0.8 ) name(g1, replace) xlabel(0 [20] 80, labsize(vsmall)) 

// Figure 2 Middle Pane
margins, at(Mil_vs_Civ_lag =(0 (5) 80) Mil_vs_Reb_dm_lag = 0  Mil_vs_Gov_lag = 0)  
marginsplot, recast(line) ciopt(color(%20)) recastci(rarea) graphregion(color(white)) title("") ytitle("") xtitle("Against Civilian (t-1)", size(small)) yscale(range(0 (0.1) 0.8 )) ylabel(0 [0.1] 0.8 ) name(g2, replace) xlabel(0 [20] 80, labsize(vsmall)) 

// Figure 2 Right Pane
margins, at(Mil_vs_Gov_lag =(0 (5) 80) Mil_vs_Reb_dm_lag = 0  Mil_vs_Civ_lag = 0)
marginsplot, recast(line) ciopt(color(%20)) recastci(rarea) graphregion(color(white)) title("") ytitle("") xtitle("Against Government (t-1)", size(small)) yscale(range(0 (0.1) 0.8 )) ylabel(0 [0.1] 0.8 ) name(g3, replace) xlabel(0 [20] 80, labsize(vsmall)) 

// Figure 2 Combined
gr combine g1 g2 g3, col (3) l1(Pr(Negotiation)) t1(Militia Activity & Peace Processes) graphregion(color(white))


// Figure 3: Predicted Probability of Peace Talks with Equal Number of Militia Attacks to Each Target all change
margins, at(Mil_vs_Reb_oth_lag =(0) Mil_vs_Civ_lag = (0)  Mil_vs_Gov_lag = (0)) at(Mil_vs_Reb_oth_lag =(20) Mil_vs_Civ_lag = (20)  Mil_vs_Gov_lag = (20)) at(Mil_vs_Reb_oth_lag =(40) Mil_vs_Civ_lag = (40)  Mil_vs_Gov_lag = (40)) at(Mil_vs_Reb_oth_lag =(60) Mil_vs_Civ_lag = (60)  Mil_vs_Gov_lag = (60)) 
marginsplot, recast(line) ciopt(color(%20)) recastci(rarea) graphregion(color(white)) title("") ytitle("Pr(Negotiation)") xtitle("Militia targetting each type (t-1)", size(small)) yscale(range(0 (0.1) 0.5 )) ylabel(0 [0.1] 0.5 ) name(g5, replace)  xscale(range(1 (1) 4 )) xlabel(1 "0" 2 "20" 3 "40" 4 "60" ) t1(Overall Militia Activity & Peace Processes)

/// Multinomial Logit

// Table 3: Multinomial Regression on Negotiations
mlogit negtype $xx1 $xx2 $core $intensity $cont2 $ts, robust cluster(DyadId) 
estat ic
mat es_ic = r(S)
local LL: display %4.1f es_ic[1,3]
local AIC: display %4.1f es_ic[1,5]
local BIC: display %4.1f es_ic[1,6]
est store x1
outreg2 x1 using table_mlogit_prep.doc, alpha(0.001, 0.01, 0.05, 0.10) symbol(***, **, *, +) stats(coef se) dec(2) addstat(LL, `LL', AIC, `AIC', BIC, `BIC') replace label sideway


// Figure 5: Predicted Probability of Bilateral, Mediated and No Negotiations by Type of Militia Violence
//Mil_vs_Reb_dm_lag Mil_vs_Civ_lag Mil_vs_Gov_lag Mil_vs_Reb_oth_lag Mil_vs_Mil_lag

// Figure 5 Left Pane: Against Rebels
margins, at(Mil_vs_Reb_dm_lag =(0 (5) 25)  Mil_vs_Reb_dm_lag = (1 (1) 5) Mil_vs_Civ_lag = 0 Mil_vs_Gov_lag = 0 Mil_vs_Reb_oth_lag = 0)
marginsplot ,  recast(line) ciopt(color(%20)) recastci(rarea) graphregion(color(white)) title("") ytitle("") xtitle("Against Rebels (t-1)", size(small)) yscale(range(0 (0.1) 0.8 )) ylabel(0 [0.1] 1 ) name(x1, replace) xlabel(0 [5] 25, labsize(vsmall)) plot1opts(lpattern("l")) plot2opts(lpattern("-")) plot3opts(lpattern(".")) legend(on cols(3) size(vsmall) symxsize(*0.30) order(6 "No Negotiation" 5 "Mediated" 4 "Bilateral")) 

// Figure 5 Mid Pane: Against Civilians
margins, at(Mil_vs_Civ_lag =(0 (5) 25) Mil_vs_Reb_dm_lag = 0 Mil_vs_Gov_lag = 0 Mil_vs_Reb_oth_lag = 0)
marginsplot,   recast(line) ciopt(color(%20)) recastci(rarea) graphregion(color(white)) title("") ytitle("") xtitle("Against Civilians (t-1)", size(small)) yscale(range(0 (0.1) 0.8 )) ylabel(0 [0.1] 1 ) name(x2, replace) xlabel(0 [5] 25, labsize(vsmall)) plot1opts(lpattern("l")) plot2opts(lpattern("-")) plot3opts(lpattern("."))


// Figure 5 Right Pane: Against Government
margins, at(Mil_vs_Gov_lag =(0 (5) 25) Mil_vs_Civ_lag = 0 Mil_vs_Reb_dm_lag = 0  Mil_vs_Reb_oth_lag = 0 ) 
marginsplot , recast(line) ciopt(color(%20)) recastci(rarea) graphregion(color(white)) title("") ytitle("") xtitle("Against Government (t-1)", size(small)) yscale(range(0 1)) ylabel(0 [0.1] 1 ) name(x3, replace) xlabel(0 [5] 25, labsize(vsmall))  plot1opts(lpattern("l")) plot2opts(lpattern("-")) plot3opts(lpattern("."))

// Figure 5: Combined
gr combine x1 x2 x3, col (3) l1(Pr(Negotiation)) t1(Militia Activity & Peace Processes) graphregion(color(white))
grc1leg x1 x2 x3, col (3) l1(Pr(Negotiation)) t1(Militia Activity & Peace Processes: Negotiation Type) graphregion(color(white)) 

//// APPENDIX S2 ////
// S. Material #2 :As a robustness check, we replicate our analysis by using a conditional logit estimator with conflict-specific intervals, which is commonly referred to as fixed-effects. Our main findings remain robust to introducing conflict fixed-effects (full results are presented in the Appendix).

///// Item 3: clogit (APPENDIX TABLE S3)
clogit Negotiation $xx1 $xx2 $xx3 $core $intensity rebno_ct lull UN  $ts odapcln PrevMed_5y Internationalized , robust cluster(DyadId) group(DyadId)
estat ic
mat es_ic = r(S)
local LL: display %4.1f es_ic[1,3]
local AIC: display %4.1f es_ic[1,5]
local BIC: display %4.1f es_ic[1,6]
est store m1
outreg2 m1 using table_appendix.doc, alpha(0.001, 0.01, 0.05, 0.10) symbol(***, **, *, +) stats(coef se) dec(2) addstat(LL, `LL', AIC, `AIC', BIC, `BIC') replace label sideway 
